﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Timers;
using System.Windows;
using Power.DAL;
using System.Threading;
using System.Windows.Threading;

namespace Power
{
    /// <summary>
    /// Scan.xaml 的交互逻辑
    /// </summary>
    public partial class Scan : Window
    {
        public Scan()
        {
            InitializeComponent();
        }

        public string username
        {
            get;
            set;
        }

        public string device
        {
            get;
            set;
        }

        public string goteninfo;
        string[] arg = new string[8];
        string[] strGPS = new string[8];

        public void ThreadToScan()
        {
            int recv;
            string DeIP;
            DeIP = new DeviceIPDAL().ToFind(device);
            byte[] data = new byte[1024];
            string stringData;
            string input;
            Socket userclient;

            IPAddress localip = IPAddress.Parse(DeIP);
            IPEndPoint ipport = new IPEndPoint(localip, 23456);

            try
            {
                userclient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                //userclient.Connect(ipport);

                string gettemp = "gettemp";
                data = Encoding.ASCII.GetBytes(gettemp);
                userclient.SendTo(data, data.Length, SocketFlags.None, ipport);
                IPEndPoint senderip = new IPEndPoint(IPAddress.Any, 0);
                EndPoint Remote = (EndPoint)senderip;
                data = new byte[1024];
                recv = userclient.ReceiveFrom(data, ref Remote);
                goteninfo = Encoding.ASCII.GetString(data, 0, recv);
            }
            catch (SocketException)
            {
                MessageBox.Show("客户端不在线");
                return;
            }
            finally
            {
            }

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            System.Timers.Timer scanTimer = new System.Timers.Timer();
            scanTimer.Elapsed += new ElapsedEventHandler(theout);  //到达时间的时候执行事件；
            // 设置引发时间的时间间隔 此处设置为1秒（1000毫秒）
            scanTimer.Interval = 5000;
            scanTimer.AutoReset = false;//设置是执行一次（false）还是一直执行(true)；
            scanTimer.Enabled = true;
                //是否执行System.Timers.Timer.Elapsed事件；

            Thread ts = new Thread(new ThreadStart(ThreadToScan));
            ts.Start();
            LaunchTimer();


        }

        public void theout(object source, System.Timers.ElapsedEventArgs e)
        {


            try
            {

                strGPS = goteninfo.Split(',');
                //MessageBox.Show(goteninfo);
                //MessageBox.Show(strGPS[1]);

                arg[0] = (string)strGPS[0];
                arg[1] = (string)strGPS[1];
                arg[2] = (string)strGPS[2];
                arg[3] = (string)strGPS[3];
                arg[4] = (string)strGPS[4];
                arg[5] = (string)strGPS[5];
                arg[6] = (string)strGPS[6];
                arg[7] = (string)strGPS[7];
                arg[8] = (string)strGPS[8];


                //MessageBox.Show(arg[2]);
                //MessageBox.Show(arg[3]);
                //arg[6] = (string)strGPS[6];
                //textBox1.Text = "";
                //textBox2.Text ="";
                //textBox3.Text = "";
                //textBox4.Text = "";

                //if (arg[0] == "111" && arg[6] == "111")
                //{


                //textBox1.Text = arg[2].ToString();
                //textBox2.Text = arg[3].ToString();
                //textBox3.Text = arg[4].ToString();
                //textBox4.Text = arg[5].ToString();
                //}
            }
            catch (Exception)
            {
                //MessageBox.Show("客户端不在线");
                return;
            }
        
        }



        private void TimerTick(object sender, EventArgs e)
        {
            textBox1.Text = arg[2];
            textBox2.Text = arg[3];
            textBox3.Text = arg[4];
            textBox4.Text = arg[6];

        }

        private void LaunchTimer()
        {
            // 判断是否在设计模式，是则返回，注意：如果不加这句话，正在设计界面的时候可能会报错哦！
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);   //间隔1秒
            timer.Tick += new EventHandler(TimerTick);
            timer.Start();
        }




        private void button2_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }
    }
}